/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cinema.client.cinemaclient.ui;

import com.cinema.client.cinemaclient.managers.UserManager;
import com.cinema.client.cinemaclient.model.User;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 *
 * @author Zsolt Szecsi
 */
public class RegisterFrame extends JFrame {

    private final static String[] ROLES = {"user", "employee"};
    private JTextField nameTxt, usernameTxt;
    private JPasswordField passwordTxt, passwordAgainTxt;
    private JComboBox roleCbx;
    private JButton okBtn, cancelBtn;
    private UserManager userManager;
    private JFrame parent;

    public RegisterFrame(JFrame parent) {
        super("Register");
        // init simple attributes    
        userManager = new UserManager();
        this.parent = parent;
        // init ui elements
        JPanel mainPanel = new JPanel();
        JLabel nameLbl = new JLabel("Name:");
        JLabel usernameLbl = new JLabel("Username:");
        JLabel passwordLbl = new JLabel("Password:");
        JLabel passwordAgainLbl = new JLabel("Password again:");
        JLabel roleLbl = new JLabel("Role: ");
        nameTxt = new JTextField();
        usernameTxt = new JTextField();
        passwordTxt = new JPasswordField();
        passwordAgainTxt = new JPasswordField();
        roleCbx = new JComboBox(ROLES);
        okBtn = new JButton("Ok");
        cancelBtn = new JButton("Cancel");

        //compose ui
        mainPanel.setLayout(new GridLayout(6, 2));
        mainPanel.add(nameLbl);
        mainPanel.add(nameTxt);
        mainPanel.add(usernameLbl);
        mainPanel.add(usernameTxt);
        mainPanel.add(passwordLbl);
        mainPanel.add(passwordTxt);
        mainPanel.add(passwordAgainLbl);
        mainPanel.add(passwordAgainTxt);
        mainPanel.add(roleLbl);
        mainPanel.add(roleCbx);
        mainPanel.add(okBtn);
        mainPanel.add(cancelBtn);

        this.setContentPane(mainPanel);
        this.setBounds(100, 100, 300, 200);
        setListeners();
    }

    private void setListeners() {
        okBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("Click listener called");
                if ("".equals(nameTxt.getText()) || "".equals(usernameTxt.getText()) || "".equals(passwordTxt.getText()) || "".equals(passwordAgainTxt.getText())) {
                    System.out.println("Click listener error1");
                    JOptionPane.showMessageDialog(RegisterFrame.this, "Error: Empty field!");
                    return;
                }
                if (!passwordTxt.getText().equals(passwordAgainTxt.getText())) {
                    System.out.println("Click listener error2");
                    JOptionPane.showMessageDialog(RegisterFrame.this, "Error: Password not equal with \"again password\"!");
                    return;
                }
                System.out.println("Click listener ok");
                User user = new User(0, nameTxt.getText(), usernameTxt.getText(), passwordTxt.getText(), ROLES[roleCbx.getSelectedIndex()]);
                userManager.insertUser(user);
                parent.setVisible(true);
                RegisterFrame.this.dispose();
            }
        });
        cancelBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                parent.setVisible(true);
                RegisterFrame.this.dispose();
            }
        });
    }

}
